define(function(require, exports, module) {
	var plugins = require('plugins');
	require('jquery');
	require('bootstrap');
	require('bootstraponhover');
	require('handlebars');
	require('slick');
	require('waterfall');
	require('masonry');
	require('waypoints');
	require('hideseek');
	require('layer');
	layer.config({
	    path: '/assets/vendors/layer/' //layer.js所在的目录，可以是绝对目录，也可以是相对目录
	});
	var timenow = new Date().getTime();
	//用户登录后鼠标经过菜单
	$(".right-part").bootstrapDropdownOnHover();
	$(".menu-bar").bootstrapDropdownOnHover();
	

	$('.slick').slick({
		dots: true,  //显示项目导航
		fade: true,  //淡入淡出
		autoplay: true  //自动播放
	});

	var id=$("#waterfall").attr("data-id");	
	$('#waterfall').waterfall({
		itemCls: 'pin', 
		fitWidthBoolean: false, //是否自适应父元素宽度
	    maxCol: undefined,
	    gutterWidth: 16,
	    gutterHeight: 16,
	    maxPage: 20,
	    align:'center',
	    minCol: 1, 
	    bufferPixel: -50, 
	    containerStyle: {
	        position: 'relative'
	    },
	    resizable: true, 
	    isFadeIn: false,
	    isAnimated: false,
	    isFadeInBoolean:false,
	    isAutoPrefill: true,
	    checkImagesLoaded: true,
	    resizableBoolean: true, //缩放时是否触发数据重排
	    state: { 
	        isDuringAjax: false, 
	        isProcessingData: false, 
	        isResizing: false,
	        curPage: 2 
	      },
	    callbacks: {
	        loadingFinished: function($loading, isBeyondMaxPage) {
	            if ( !isBeyondMaxPage ) {
	                $loading.fadeOut();
	            } else {
	                $loading.hide();
	                $('#waterfall').show();
	            }
	        },
	        renderData: function (data, dataType) {
	            var tpl,
	                template,
	                resultNum = data.data.pageCount;
	            //alert(page);
	            if ( data.data.pageNum > resultNum || resultNum > 20) {
	                $('#waterfall').waterfall('pause', function() {
	                    $('.waterfall-loading').html('<p style="color:#666;">数据加载完毕...</p>')
	                });
	                //alert(resultNum); 
	                $('#waterfall').after('<div class="loading" style="display: block;"><img src="/assets/images/end.png"></div>');
	                $('.waterfall-loading').hide();
	            }else{
		            if ( dataType === 'json' ||  dataType === 'jsonp'  ) { // json or jsonp format
		                tpl = $('#waterfall-pin').html();
		                template = Handlebars.compile(tpl); 
		                return template(data);
		            } else { // html format
		                return data;
		            }
	            }  
	        }
	    },
	    path: function(page) {
	        return '/category.json?classId='+id+'&p=' + page;
	    }
	});
	
	var id=$("#albumWaterfall").attr("data-id");	
	$('#albumWaterfall').waterfall({
		itemCls: 'pin', 
		fitWidthBoolean: false, //是否自适应父元素宽度
	    colWidth: 236,
	    maxCol: undefined,
	    gutterWidth: 16,
	    gutterHeight: 16,
	    maxPage: 10,
	    align:'center',
	    minCol: 1, 
	    bufferPixel: -50, 
	    containerStyle: {
	        position: 'relative'
	    },
	    resizable: true, 
	    isFadeIn: false,
	    isAnimated: false,
	    isFadeInBoolean:false,
	    isAutoPrefill: true,
	    checkImagesLoaded: true,
	    resizableBoolean: true, //缩放时是否触发数据重排
	    state: { 
	        isDuringAjax: false, 
	        isProcessingData: false, 
	        isResizing: false,
	        curPage: 2 
	      },
	    callbacks: {
	        loadingFinished: function($loading, isBeyondMaxPage) {
	            if ( !isBeyondMaxPage ) {
	                $loading.fadeOut();
	            } else {
	                $loading.hide();
	                $('#albumWaterfall').show();
	            }
	        },
	        renderData: function (data, dataType) {
	            var tpl,
	                template,
	                resultNum = data.data.pageCount;
	            
	            if (data.data.pageNum > resultNum || resultNum > 10) {
	                $('#albumWaterfall').waterfall('pause', function() {
	                    $('.waterfall-loading').html('<p style="color:#666;">数据加载完毕...</p>')
	                });
	                //alert(page); 
	                $('#albumWaterfall').after('<div class="loading" style="display: block;"><img src="/assets/images/end.png"></div>');
	                $('.waterfall-loading').hide();
	            }else{
	            	if ( dataType === 'json' ||  dataType === 'jsonp'  ) { // json or jsonp format
		                tpl = $('#waterfall-pin').html();
		                template = Handlebars.compile(tpl); 
		                return template(data);
		            } else { // html format
		                return data;
		            }
	            }
	        }
	    },
	    path: function(page) {
	        return '/album/pin_view.json?id='+id+'&p=' + page;
	    }
	});

	//瀑布流评论点击下拉
	$(document).on('click', '#addreview', function (){
		var uname=$(this).attr("data-name");
		var uid=$(this).attr("user-id");
		var infoid=$(this).attr("data-id");
		var pinbox=$(this).parent().parent().parent().parent();
		var leftnum=$(this).parent().parent().parent().parent().position().left;
		$.each(pinbox.nextAll(), function(){
			if ($(this).css("left") == leftnum+"px") { 
				if(pinbox.find("#addcomment").length==0){
					var topnum=$(this).position().top;
				    $(this).css('top',(topnum+108)+'px');
				}
			}
		});
		if(pinbox.find("#addcomment").length==0){
		    pinbox.append("<div id=\'addcomment\' class=\'write convo clearfix\'><a href=\'/u/"+uid+"\' title=\'"+uname+"\' class=\'img x\'><img src=\'"+smallAvatar+"\'></a><input type=\"hidden\" id=\"touid\" name=\"touid\" value=\"\"><textarea placeholder=\'添加评论或把采集@给好友\' class=\'clear-input GridComment\'></textarea><a href=\'#\' onclick=\'return false;\' data-id=\'"+infoid+"\' class=\'grid_comment_button btn\' style=\'display: block;\'><span class=\'text\'> 添加评论</span></a></div>");
		}
		
		$.each($('#waterfall .pin'), function(){
			var p1Bottom = $('#waterfall').offset().top+$('#waterfall').height(),
			p2Bottom = $(this).offset().top+$(this).height();
			if(p2Bottom>p1Bottom){
				pinbox.parent().css('height',(p2Bottom-p1Bottom+$('#waterfall').height()+20)+'px');
			}
		});
	});
	
	//瀑布流点击回复
	$(document).on('click', '#addreply', function (){
		var pinbox=$(this).parent().parent().parent();
		var leftnum=$(this).parent().parent().parent().position().left;
		var uname=$(this).attr("data-name");
		var uid=$(this).attr("user-id");
		var infoid=$(this).attr("data-id");
		$.each(pinbox.nextAll(), function(){
			if ($(this).css("left") == leftnum+"px") { 
				if(pinbox.find("#addcomment").length==0){
					var topnum=$(this).position().top;
				    $(this).css('top',(topnum+108)+'px');
				}
			}
		});
		if($(this).parent().parent().parent().find("#addcomment").length==0){
			pinbox.append("<div id=\'addcomment\' class=\'write convo clearfix\'><a href=\'#\' title=\'\' class=\'img x\'><img src=\'"+smallAvatar+"\'></a><input type=\"hidden\" id=\"touid\" name=\"to_uid\" value=\"\"><textarea placeholder=\'添加评论或把采集@给好友\' class=\'clear-input GridComment\'></textarea><a href=\'#\' onclick=\'return false;\' data-id=\'"+infoid+"\' class=\'grid_comment_button btn\' id=\"review-add\" style=\'display: block;\'><span class=\'text\'> 添加评论</span></a></div>");
		}
		$.each($('#waterfall .pin'), function(){
			var p1Bottom = $('#waterfall').offset().top+$('#waterfall').height(),
			p2Bottom = $(this).offset().top+$(this).height();
			if(p2Bottom>p1Bottom){
				pinbox.parent().css('height',(p2Bottom-p1Bottom+$('#waterfall').height()+20)+'px');
			}
		});
		pinbox.children().find(".clear-input").attr('placeholder','回复'+uname+'：').focus();
		pinbox.children().find("#touid").val(uid);
	});	
	
	$(document).on('focus', '.GridComment', function (){
		var pinbox=$(this).parent().parent();
		$($(this).parent().parent().siblings()).each(function() { 
			if($(this).children().children(".grid_comment_button").css("display")=="block"){
				var leftcomment=$(this).position().left;
					$.each($(this).nextAll(), function(){
						if ($(this).css("left") == leftcomment+"px") {
				            var topnum=$(this).position().top;
							$(this).css('top',(topnum-40)+'px');
						}
					});
				$(this).children().children(".grid_comment_button").hide();
            }
        });

		var leftnum=$(this).parent().parent().position().left;
		$.each($(this).parent().parent().nextAll(), function(){
			if ($(this).css("left") == leftnum+"px") { 
				if(pinbox.children().children(".grid_comment_button").css("display")=="none"){
					var topnum=$(this).position().top;
				    $(this).css('top',(topnum+40)+'px');
				}
			}
		});
		$(this).next(".grid_comment_button").show();
	});
	
	//用户添加评论
	$(document).on('click', '.grid_comment_button', function (){
		var pinbox=$(this).parent().parent();
		var leftnum=$(this).parent().parent().position().left;
		
		var id=$(this).attr("data-id");
		var content=$(this).prev().val();
		var touid=pinbox.children().children("#touid").val();
		
		$.ajax({
    		url: '/member/addComment.json', 
    		data: {'id': id,'content': content,'touid': touid},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('发送错误', {icon: 2});
    		},
    		success: function(data){	
    			if(data.code==0){
    				//弹出提示5秒后刷新页面
    				layer.msg(data.message,{icon:1,time:5000},function(){});
    				pinbox.children().children("#touid").val("");
    				pinbox.children().children(".clear-input").attr('placeholder','添加评论或把最新时装@给好友').focus();
    				pinbox.children().children(".clear-input").val("");
    				var review="";
					if(data.data.toUid!=null){
						review="<span>回复   <a href=\"/u/"+data.data.toUid+"/\" class=\"author\">"+data.data.toNickname+"</a>： </span>";
					}
					pinbox.children(".comments").append("<div class=\'comment convo clearfix\'><a href=\'/u/"+data.data.userId+"\' title=\'"+data.data.nickname+"\' class=\'img x\'><img src=\'"+data.data.face+"\' class=\'avt\'></a><div class=\'content\'><a href=\'/u/"+data.data.userId+"/\' class=\'author\'>"+data.data.nickname+"</a>:&nbsp;"+review+data.data.content+"</div><a title=\'回复\' id=\'addreply\' data-name=\'"+data.data.nickname+"\' user-id=\'"+data.data.userId+"\' data-id=\'"+data.data.id+"\' class=\'replyButton\'></a></div>");
					//alert(pinbox+"-------"+leftnum); 
					//console.log(touid+"---------");
					//var ctheight=pinbox.children(".comments").height();
					//console.log(ctheight);
					$.each(pinbox.nextAll(), function(){
						if ($(this).css("left") == leftnum+"px") { 
							var topnum=$(this).position().top;
							$(this).css('top',(topnum+68)+'px');
						}
					});
					
					$.each($('#waterfall .pin'), function(){
						var p1Bottom = $('#waterfall').offset().top+$('#waterfall').height(),
						p2Bottom = $(this).offset().top+$(this).height();
						if(p2Bottom>p1Bottom){
							pinbox.parent().css('height',(p2Bottom-p1Bottom+$('#waterfall').height()+ctheight)+'px');
						}
					});
    				return false;
    			}else{
    				layer.msg(data.message, {icon: 2});
				}
          	}
    	});
	});
	
	
	//添加专辑弹窗
	$(document).on('click', '.repin', function (){
		var id=$(this).attr("data-id");
		$.get("/pin/pin_view.json", { id: id },
		    function(data){
			result=data.data.imgurl;
			if ( $("#repin_dialog").length < 1 ) { 
				var title=data.data.title;
				if(title==null || title==""){
					title="";
				}
				$(".dialog-boxes").append("<div class=\"dialog-overlay\"></div><div id=\"repin_dialog\" data-name=\"repin\" class=\"dialog-box\"><div class=\"box-title\">收藏</div><div class=\"box-inner\"><div id=\"uniq_498927\" class=\"pin-create\"><div class=\"preview\" style=\"padding-top: 70px;\"><div class=\"image\"><img src=\""+data.data.imgurl+"\" id=\"imgurl\"></div><textarea placeholder=\"点击添加描述...\" id=\"content\" class=\"description\">"+data.data.content+"</textarea></div><div class=\"right-part\" id=\"album-list\"><input id=\"search-nodata\" class=\"clear-input search-input\" name=\"search-nodata\" placeholder=\"搜索或创建专辑\" type=\"text\" data-list=\".nodata_list\" autocomplete=\"off\"><div style=\"display: none; margin-bottom: 10px;\" class=\"common-message info\"></div><div class=\"board-divider\"></div><div class=\"boardlist\"><div class=\"scrollable\"><ul class=\"vertical nodata_list\"></lu><div class=\"sorted\"></div><div class=\"sorted\"></div></div></div><div class=\"text-block-divider\"></div><div class=\'text-block\'><div style=\'display: none;\' class=\'createboard\'><i class=\'icon add\'></i>创建专辑 “<span class=\'text\'></span>”</div><div class=\"tag-inputs\"><input type=\"hidden\" name=\"tags\" id=\"fieldTags\" value=\"\"><ul id=\"tags\" class=\"tagit ui-widget ui-widget-content ui-corner-all\"></ul></div><div class=\'tag-bar\'><div style=\'display: block;\' class=\'normal\'><span class=\'title\'>推荐标签</span><span class=\'tags\'><div class=\'tag\'>妩媚</div><div class=\'tag\'>小清新</div><div class=\'tag\'>可爱</div><div class=\'tag\'>齐刘海</div><div class=\'tag\'>模特</div><div class=\'tag\'>诱惑</div></span></div></div></div></div><div class=\"bottom-part\"><input id=\"add_pin_id\" name=\"pin_id\" value=\""+id+"\" type=\"hidden\"><input id=\"add_album_id\" name=\"album_id\" value=\"\" type=\"hidden\"><div class=\"buttons\"><a href=\"#\" onclick=\"return false;\" class=\"addpin btn btn18 rbtn \"><span class=\"text\"> 确认收藏</span></a></div></div></div></div><div class=\"close-btn\"><i></i></div></div>");
			}else{
				$(".dialog-overlay").show();
				$("#repin_dialog").show();
				$("#imgurl").attr("src",data.data.imgurl);
				$("#content").val(data.data.content);
				$("#add_pin_id").val(id);
			};
			$(".vertical").html("");
			if(data.data.albumList!=null){
				$.each(data.data.albumList, function(k, v) {
					$(".vertical").append("<li class=\"item\" style=\"display:list-item;\" data-id=\""+v.albumId+"\">"+v.albumname+"<div class=\"controller\"></div></li>");
				});
			}
			$('#search-nodata').hideseek({
			    nodata: '没有查询到相关专辑可点击下面添加'
			});
			$($(".vertical").children("li").get(0)).addClass("selected");
			$("#add_album_id").val($($(".vertical").children("li").get(0)).attr("data-id"));
			$($(".vertical").children("li").get(0)).prepend("<i class=\"icon history\"></i>");
			$("#tags").html("");
			$("#fieldTags").val("");
			//标签
			$('#tags').tagit({
		        singleField: true,
		        singleFieldNode: $('#fieldTags'),
		        tagLimit: 5
		    });
		});
	});
	

	
	//确定选择专辑
	$(document).on('click', '.item', function (){
		$(".item").removeClass("selected");
		$(this).addClass("selected");
		$("#add_album_id").val($(this).attr("data-id"));
	});
	
	//隐藏添加专辑弹窗
	$(document).on('click', '.close-btn', function (){
		$(".dialog-overlay").hide();
		$("#repin_dialog").hide();
	});
	
	//查询是否有该专辑
	$(document).on('keyup', '#search-nodata', function (){
		var text=$("#search-nodata").val();
		if($(".item:contains('"+text+"')").length==0){
			$(".createboard").show();
			$(".createboard > .text").html($.trim(text));
			//console.log("无该关键词");
		}else{
			$(".createboard").hide();
		}
	});
	
	//查询是否有该专辑
	$(document).on('keyup', '#search-nodata', function (){
		var text=$("#search-nodata").val();
		if(text.length==0){
			$(".createboard").hide();
		}
	});
	
	//弹窗位置添加专辑
	$(document).on('click', '.createboard', function (){
		var text=$("#search-nodata").val();
		$.ajax({
    		url: '/member/album/add.json?'+timenow, 
    		data: {'albumname': $.trim(text)},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				$(".vertical").append("<li class=\"item\" style=\"display: list-item;\" data-id=\""+data.data.albumId+"\">"+data.data.albumname+"<div class=\"controller\"></div></li>");
    				$(".createboard").hide();
    				$(".createboard > .text").html("");
    				$(".no-results").remove();
    				$("#add_album_id").val("");
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	//图集添加进用户专辑
	$(document).on('click', '.addpin', function (){
		var albumId=$("#add_album_id").val();
		var pinId=$("#add_pin_id").val();
		var content=$("#content").val();
		var tags=$("#fieldTags").val();
		if(albumId==null||albumId==""){
			layer.msg('请点选专辑后再收藏！', {icon: 2});
			return false;
		}
		$.ajax({
    		url: '/album/pins.json?'+timenow, 
    		data: {'albumId': albumId,'pinId': pinId,'content': content,'tags': tags},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				$(".dialog-overlay").hide();
    				$("#repin_dialog").hide();
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	//添加用户关注的专辑
	$(document).on('click', '.follow-album', function (){
		var albumId=$(this).attr("data-id");
		if(albumId==null||albumId==""){
			layer.msg('请点选专辑后再收藏！', {icon: 2});
			return false;
		}
		$.ajax({
    		url: '/album/fans.json?'+timenow, 
    		data: {'albumId': albumId},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				if($(".follow-album").hasClass("unfollow")){
    					$(".follow-album").addClass("rbtn");
    					$(".follow-album").removeClass("unfollow");
    					$(".follow-album").children(".text").html("关注此专辑");
    					if(parseInt($(".tab").html().replace(/[^0-9]/ig,""))>0){
    						$(".tab").html("被"+(parseInt($(".tab").html().replace(/[^0-9]/ig,""))-1)+"人关注");
    					}
    				}else{
    					$(".follow-album").removeClass("rbtn");
    					$(".follow-album").addClass("unfollow");
    					$(".follow-album").children(".text").html("取消关注此专辑");
    					$(".tab").html("被"+(parseInt($(".tab").html().replace(/[^0-9]/ig,""))+1)+"人关注");
    				}
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
	
	//点击其他区域收缩分类菜单
	$(document).bind("click",function(e){ 
		var target = $(e.target); 
		if(target.closest(".BoardSelector").length == 0){ 
			$(".BoardList").hide(); 
		} 
	});
	
	//点击下来菜单
	$(document).on('click', '.CurrentBoard', function (){
		$(".CurrentBoard").parent().parent().children(".BoardList").show(); 
	});
	
	//点选分类
	$(document).on('click', '.BoardCategory', function (){
		$(this).parent().parent().parent(".BoardList").hide();
		$(this).parent().parent().parent().prev().children(".CurrentBoard").html($(this).text()); 
		var classId=$(this).attr("data-id");
		$("#classId").val(classId);
		if($("#classId").val(classId)!=""||$("#classId").val(classId)!=null){
			$("#ajaxtype").removeClass('disabled');
		}
	});
	
	//提交修改专辑所属分类
	$(document).on('click', '#ajaxtype', function (){
		var albumId=$(this).attr("data-id");
		var classId=$("#classId").val();
		if(classId==null||classId==""){
			layer.msg('请选择分类后再提交！', {icon: 2});
			return false;
		}
		$.ajax({
    		url: '/album/updatetype.json?'+timenow, 
    		data: {'albumId': albumId,'classId': classId},
    		dataType: "json",
    		type :  "POST",
    		cache : false,
    		async: false,
    		error : function(i, g, h) {
    			layer.msg('请登录后操作', {icon: 2});
    		},
    		success: function(data){	
    			if (data.code==0) {
    				layer.msg(data.message, {icon: 1});
    				window.location.reload();
    			}else{
    				layer.msg(data.message, {icon: 2});
    			}
          	}
    	});
	});
});